import subprocess
# 假设原文件名为 mail_script.py，与本脚本在同一目录下
file_path = "C:\\wap\\email_upload\\uploaer.py"

# 读取原文件内容，指定编码为 'utf-8'
with open(file_path, 'r', encoding='utf-8') as file:
    lines = file.readlines()

sender_email_line = None
sender_password_line = None
receiver_email_line = None

# 找到包含邮箱信息的行
for index, line in enumerate(lines):
    if "sender_email =" in line:
        sender_email_line = index
    elif "sender_password =" in line:
        sender_password_line = index
    elif "receiver_email =" in line:
        receiver_email_line = index

# 提示信息
print("目前仅支持QQ邮箱")
# 获取用户输入的新邮箱信息
new_sender_email = input("请输入新的发件人邮箱账号: ")
new_sender_password = input("请输入新的发件人邮箱授权码: ")
new_receiver_email = input("请输入新的收件人邮箱: ")

# 修改相应行的内容
lines[sender_email_line] = f"sender_email = \"{new_sender_email}\"\n"
lines[sender_password_line] = f"sender_password = \"{new_sender_password}\"\n"
lines[receiver_email_line] = f"receiver_email = \"{new_receiver_email}\"\n"

# 将修改后的内容写回到原文件
with open(file_path, 'w', encoding='utf-8') as file:
    file.writelines(lines)

print("邮箱信息已成功修改！")

# 构造要在cmd中执行的命令
command = f'py {file_path}'
try:
    result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
    print("邮件发送程序输出：")
    print(result.stdout)
    print("测试邮件已发送，请注意查看是否正常接收。")
except subprocess.CalledProcessError as e:
    print(f"运行邮件发送程序时出错: {e}")
    print(f"错误输出: {e.stderr}")
